﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;
using xClient.Framework.StaticResource;

namespace cMES1009
{
    public partial class frmGroupKPNEdit : Form
    {
        private DataGridViewRow _dgvRow = null;
        private string _strCmd = "ADD";
        public frmGroupKPNEdit(string vCMD, object param = null)
        {
            InitializeComponent();
            //initial group name list;
            InitialGroupAndCodeList();

            this._strCmd = vCMD;
            if (_strCmd.ToUpper().Equals("ADD"))
            {
                this.ClearFormInputData();
                this.Text = "新增工站捡料信息";
            }
            else if (_strCmd.ToUpper().Equals("EDIT"))
            {
                this.Text = "修改工站捡料信息";
                this.lblId.Visible = true;
                this.txtId.Visible = true;
                this.txtId.Enabled = false;
                this.cmbGroupName.Enabled = false;

                if (param != null)
                {
                    this._dgvRow = (DataGridViewRow)param;
                }
                this.InitialViewData();

                this.txtUpdateUser.Text = StaticDictionary.Dictionary.UserID;
                this.dtpUpdateTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            }
        }

        private void InitialViewData()
        {
            if(this._dgvRow != null)
            {
                this.txtId.Visible = true;
                this.lblId.Visible = true;
                this.txtId.Text = _dgvRow.Cells[0].Value.ToString();
                this.cmbGroupName.Text = _dgvRow.Cells[1].Value.ToString();
                this.txtStation.Text = _dgvRow.Cells[2].Value.ToString();
                this.combKPNCode.Text = _dgvRow.Cells[3].Value.ToString();
                this.txtRemarks.Text = _dgvRow.Cells[4].Value.ToString();
                this.txtIsValid.Text = _dgvRow.Cells[5].Value.ToString();                
                this.dtpCreateTime.text = _dgvRow.Cells[6].Value.ToString();
                this.txtCreateUser.Text = _dgvRow.Cells[7].Value.ToString();
                this.dtpUpdateTime.text = _dgvRow.Cells[8].Value.ToString();
                this.txtUpdateUser.Text = _dgvRow.Cells[9].Value.ToString();                
            }
        }

        private void ClearFormInputData()
        {
            this.lblId.Visible = false;
            this.txtId.Visible = false;
            this.cmbGroupName.Text = "";
            this.txtStation.Text = "";
            this.txtRemarks.Text = "";
            this.combKPNCode.Text = "";
            this.txtId.Text = "";
            this.txtRemarks.Text = "";
            this.txtCreateUser.Text = StaticDictionary.Dictionary.UserID;
            this.txtUpdateUser.Text = "";
            this.dtpCreateTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            this.cmbGroupName.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInputValidate())
            {
                return;
            }
            var _param = new Dictionary<string, object>();
            _param.Add("id", this.txtId.Text.Trim());
            _param.Add("group_name", this.cmbGroupName.Text.Trim().ToUpper());
            _param.Add("station_name", this.txtStation.Text.Trim().ToUpper());
            _param.Add("kpn_code", this.combKPNCode.Text.Trim().ToUpper());
            _param.Add("remarks", this.txtRemarks.Text.Trim());
            _param.Add("create_user", this.txtCreateUser.Text.Trim());
            if (this._strCmd.Equals("ADD"))
            {
                _param.Add("create_time", this.dtpCreateTime.Text);
            }
            else if (this._strCmd.Equals("EDIT"))
            {
                _param.Add("update_user", this.txtCreateUser.Text.Trim());
                _param.Add("update_time", this.dtpUpdateTime.Text);
                _param.Add("is_valid", this.txtIsValid.Text);
            }

            var dataModel = JsonConvert.SerializeObject(_param);
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            if (this._strCmd.Equals("ADD"))
            {
                result = Services.ServiceMethod.AddGroupKPNInfo(dataModel);
            }
            else if(this._strCmd.Equals("EDIT"))
            {
               result  = Services.ServiceMethod.UpdateGroupKPNInfo(dataModel);
            }
            
            if (result.Status)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(result.Message, "系统消息");
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private bool CheckInputValidate()
        {
            bool _bResult = true;
            if (String.IsNullOrEmpty(this.cmbGroupName.Text.Trim()))
            {
                MessageBox.Show("站别必须输入.");
                this.cmbGroupName.Focus();
                return false;
            }
            else if (String.IsNullOrEmpty(this.combKPNCode.Text.Trim()))
            {
                MessageBox.Show("物料类别必须输入.");
                this.combKPNCode.Focus();
                return false;
            }
            return _bResult;
        }

        private void InitialGroupAndCodeList()
        {
            var result = Services.ServiceMethod.GetGroupNameList();
            if (result.Status)
            {
                this.cmbGroupName.DataSource = result.Data;
                this.cmbGroupName.ValueMember = "group_name";
                this.cmbGroupName.DisplayMember = "group_name";
            }

            var _kpnCode = this.combKPNCode.Text.ToUpper().Trim();
            result = Services.ServiceMethod.GetKPNCodeList(_kpnCode);
            if (result.Status)
            {
                this.combKPNCode.Items.Clear();
                foreach (var tmpCode in result.Data)
                {
                    this.combKPNCode.Items.Add(tmpCode.part_code);
                }
            }
        }
    }
}
